package delegate4j;

import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;

import java.util.ArrayList;
import java.util.concurrent.FutureTask;

public class AsyncDelegateCallback extends Delegate<IAsyncDelegateCallback> {
    public AsyncDelegateCallback(IAsyncDelegateCallback delegate) {
        super(delegate);
    }

    public void invoke(@NotNull Object sender, @Nullable Object[] args, @Nullable Object result) throws Exception {
        for (int i = 0; i < delegates.size(); i++) {
            delegates.get(i).invoke(sender, args, result);
        }
    }

    public AsyncResult<Void> invokeAsync(@NotNull Object sender, @Nullable Object[] args, @Nullable Object result) {
        ArrayList<FutureTask<Void>> tasks = new ArrayList<>();
        for (int i = 0; i < delegates.size(); i++) {
            tasks.add(delegates.get(i).invokeAsync(sender, args, result));
        }

        //返回tasks的浅拷贝
        return new AsyncResult<>(tasks);
    }
}
